package org.gruposp2p.dnie.client.json;

import java.util.List;
import org.gruposp2p.dnie.client.dto.DocumentToSignOptionDTO;
import org.gruposp2p.dnie.client.dto.DocumentToSignDTO;
import org.gruposp2p.dnie.client.util.DateUtils;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import static org.gruposp2p.dnie.client.json.JSONConstants.*;

public class DTOToJSON {
	
    public static JSONObject getDocumentToSignJSON (DocumentToSignDTO documentToSign) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(DOCUMENT_TO_SIGN_ID,
                new JSONString(documentToSign.id));        
        jsonObject.put(DOCUMENT_TO_SIGN_SIGNATURE_DATA_TYPE,
                new JSONString(documentToSign.type));
        jsonObject.put(DOCUMENT_TO_SIGN_URL,
                new JSONString(documentToSign.url));
        jsonObject.put(DOCUMENT_TO_SIGN_CONTENT,
                new JSONString(documentToSign.content));
        jsonObject.put(DOCUMENT_TO_SIGN_TITLE,
                new JSONString(documentToSign.title));
        jsonObject.put(DOCUMENT_TO_SIGN_SIGNATURE_OPTION_SELECTION_TYPE,
                new JSONString(documentToSign.optionSelectionType));                
        jsonObject.put(DOCUMENT_TO_SIGN_DATE_BEGIN,
                new JSONString(DateUtils.getStringFromDate(documentToSign.dateBegin)));
        jsonObject.put(DOCUMENT_TO_SIGN_DATE_END,
                new JSONString(DateUtils.getStringFromDate(documentToSign.dateEnd)));
        if (documentToSign.type.equals(DocumentToSignDTO.DOCUMENT_TYPE_VOTE)) {
            jsonObject.put(DOCUMENT_TO_SIGN_OPTIONS,
                    getOptionsJSON(documentToSign.options));
        }
        return jsonObject;
    }
	
    public static String getDocumentToSignJSONString (DocumentToSignDTO documentToSign) {
        return getDocumentToSignJSON(documentToSign).toString();
    }

    public static JSONObject getOptionsJSON (List<DocumentToSignOptionDTO> options) {
    	JSONObject jsonObject = new JSONObject();
    	JSONArray jsonArray = new JSONArray();
        int index = 0;
        for (DocumentToSignOptionDTO optionDTO : options) {
        	jsonArray.set(index++, getOptionJSON(optionDTO));
        }
        jsonObject.put(DOCUMENT_TO_SIGN_OPTION, jsonArray);
        return jsonObject;
    }

    public static JSONObject getOptionJSON (DocumentToSignOptionDTO option) {
        JSONObject jsonObject = new JSONObject();

        jsonObject.put(DOCUMENT_TO_SIGN_OPTION_CONTENT,
                new JSONString(option.content));
        return jsonObject;
    }

        /*
    public static JSONArray getCommentsJSON (List<CommentDTO> comments) {
        JSONArray jsonArray = new JSONArray();
        int index = 0;
        for (CommentDTO commentDTO : comments) {
                jsonArray.set(index, getCommentJSON(commentDTO));
                index++;
        }
        return jsonArray;
    }

    public static JSONObject getCommentJSON (CommentDTO comment) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(DOCUMENT_TO_SIGN_COMMENT_DATE,  new JSONString(DateUtils.getStringFromDate(comment.date)));
        jsonObject.put(DOCUMENT_TO_SIGN_COMMENT_USERID,  new JSONString(comment.userId));
        jsonObject.put(DOCUMENT_TO_SIGN_COMMENT_CONTENT,  new JSONString(comment.content));
        return jsonObject;
    }*/
	
}